package com.app.twth.ui.login;

import androidx.lifecycle.MutableLiveData;

import com.app.twth.app.Constant;
import com.app.twth.app.MyApplication;
import com.app.twth.base.BaseViewModel;
import com.app.twth.bean.BaseResponseEntity;
import com.app.twth.bean.LoadingBean;
import com.app.twth.net.JsonCallBack;
import com.app.twth.utils.PreferencesUtil;
import com.lzy.okgo.model.Response;


/**
 * ****************************************************************
 * 文件名称: LoginViewModel
 * 文件描述:
 * 注意事项:
 * 版权声明:
 * ****************************************************************
 */
public class LoginViewModel extends BaseViewModel {

    public MutableLiveData<String> userName = new MutableLiveData<>();
    public MutableLiveData<String> password = new MutableLiveData<>();
    //登录
    public MutableLiveData<String> loginResultCode = new MutableLiveData<>();//0成功；other失败
    public String loginResultMsg;
    public UserEntity loginResultData;

    public LoginViewModel() {
        mModel = new LoginDataModel();
//        mModel = new MyModel();
//            //测试数据
        userName.setValue(PreferencesUtil.getString(MyApplication.getAppInstance(), Constant.USER_NAME));
//        password.setValue("TwThAdmin1");
    }

    public void login() {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
//        ((MyModel) mModel).login(userName.getValue(), password.getValue(), new HttpListener<UserEntity>() {
//            @Override
//            public void onSuccess(UserEntity data) {
//                isLoading.postValue(false);
//                Log.e("", "onSuccess:" + data.toString());
//                loginResultData = data;
//                loginResultCode.postValue("200");
//            }
//
//            @Override
//            public void onError(String errorMsg, String code) {
//                isLoading.postValue(false);
//                Log.e("", "onError:" + errorMsg + ",code:" + code);
////                Log.e("","errorMsg:"+errorMsg+",code:"+code);
//                loginResultMsg = errorMsg;
//                loginResultCode.postValue(code);
//            }
//        });

        ((LoginDataModel) mModel).login(userName.getValue(), password.getValue(), new JsonCallBack<BaseResponseEntity<UserEntity>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<UserEntity>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<UserEntity> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    loginResultData = baseResponseEntity.data;
                    loginResultCode.setValue(baseResponseEntity.getCode());
                    return;
                }
                loginResultMsg = baseResponseEntity.getMessage();
                loginResultCode.setValue(baseResponseEntity.getCode());
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                loginResultMsg = error;
                loginResultCode.setValue(error);
            }
        });
    }
}
